<?php

class AdmRoleController extends BaseController
{
	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new AdmRole;

		// Uncomment the following line if AJAX validzao_idis needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['AdmRole']))
		{
			//print_r($_POST);exit;
			$model->attributes=$_POST['AdmRole'];
			$privilege = $_POST['privilege'];
			if($model->save()){
				//如果有设置权限，刚保存权限信息
				if(!empty($privilege)){
					foreach($privilege as $val){
						foreach($val as $val2){
							$privilege = new AdmPrivilege();
							$privilege->parent = $model->zao_id;
							$privilege->child = $val2;
							if(!$privilege->save()){
								//失败跳转
								$this->render('../index/error',array(
										'message'=>'对不起，角色权限创建失败，请稍后再试！',
										'links'=>array(
												array('角色管理页面',$this->pre_module_url.'/admRole/admin'),
										),
								));
								exit;
							}
						}
					}
				}
			    //成功跳转
			    $this->redirect(array('admin'));
			}else{
				//失败跳转
			    $this->render('../index/error',array(
										'message'=>'对不起，角色权限创建失败，请稍后再试！',
										'links'=>array(
												array('角色管理页面',$this->pre_module_url.'/admRole/admin'),
										),
				));
			    exit;
			}
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['AdmRole']))
		{
			//print_r($_POST);exit;
			$model->attributes=$_POST['AdmRole'];
			$privilege = $_POST['privilege'];
			if($model->save()){
				//删除旧的权限信息
				$criteria=new CDbCriteria;
				$criteria->compare('parent',$model->zao_id);
				AdmPrivilege::model()->deleteAll($criteria);
				//如果有设置权限，刚保存权限信息
				if(!empty($privilege)){
					foreach($privilege as $val){
						foreach($val as $val2){
							$privilege = new AdmPrivilege();
							$privilege->parent = $model->zao_id;
							$privilege->child = $val2;
							if(!$privilege->save()){
								//失败跳转
								$this->render('../index/error',array(
										'message'=>'对不起，角色权限创建失败，请稍后再试！',
										'links'=>array(
												array('角色管理页面',$this->pre_module_url.'/admRole/admin'),
										),
								));
								exit;
							}
						}
					}
				}
			    //成功跳转
			    $this->redirect(array('admin'));
			}else{
				//失败跳转
			    $this->render('../index/error',array(
										'message'=>'对不起，角色权限创建失败，请稍后再试！',
										'links'=>array(
												array('角色管理页面',$this->pre_module_url.'/admRole/admin'),
										),
				));
			    exit;
			}
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * 单条批量删除
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete()
	{
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zao_id in ($id)");

		//执行操作
		if(AdmRole::model()->deleteAll($criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}


	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new AdmRole('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['AdmRole']))
			$model->attributes=$_GET['AdmRole'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=AdmRole::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='adm-role-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
